/*
 * Copyright (c) 2008 Sergey Rudchenko <sergey.rudchenko@gmail.com>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "battle_scene.h"
#include "game.h"
#include "scene.h"
#include "controller.h"
#include "sprite.h"
#include "ship.h"
#include "sky.h"
#include "soldier.h"
#include "log.h"

#include <stdlib.h>

struct battle_scene
{
    struct ship* ship;
    struct soldier* soldier;
};

int battle_scene_update(struct scene* scene, struct game* ctx, Uint32 dticks)
{
    struct battle_scene* bs = scene->_data;
    struct ship* ship = bs->ship;
    struct soldier* soldier = bs->soldier;
    struct sky* sky = ctx->global_data;

    if (controller_check_action(ACTION_QUIT))
    {
        printf_log("ACTION_QUIT\n");
        return 0; 
    }

    sky_update(sky, ctx, dticks);
    ship_update(ship, ctx, dticks);
    soldier_update(soldier, ctx);

    ship_blit(ship, ctx, sky->image);
    soldier_blit(soldier, ctx, sky->image);
    return 1;
}

void battle_scene_free_data(void* data)
{
    struct battle_scene* bs = data;
    free_ship(bs->ship);
}

static void battle_scene_init(struct battle_scene* bs, struct game* ctx)
{
    bs->ship = new_ship();

    struct sprite* spr = bs->ship->sprite;
    sprite_move(spr, (ctx->area.w - spr->image->w) / 2,
                      ctx->area.h - spr->image->h);

    bs->soldier = new_soldier();
    spr = bs->soldier->sprite;
    sprite_move(spr, ctx->area.w / 2, ctx->area.h / 2);
}

struct scene* new_battle_scene(struct game* ctx)
{
    struct scene* obj = malloc(sizeof(struct scene));
    obj->update    = battle_scene_update;
    obj->free_data = battle_scene_free_data;

    obj->_data  = malloc(sizeof(struct battle_scene));
    battle_scene_init(obj->_data, ctx);

    return obj;
}

